﻿using MvcApplication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebApplication4MVC.Controllers
{
    public class CommonController : Controller
    {
        public UserInfoModel CurrentUserInfo { get; set; }


        /// <summary>
        /// 调用路由之前是执行
        /// </summary>
        /// <param name="filterContext"></param>

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequestBase httpRequestBase = filterContext.HttpContext.Request;
            CurrentUserInfo = Session["LoginUserInfo"] as UserInfoModel;
            if (CurrentUserInfo == null||string.IsNullOrWhiteSpace(CurrentUserInfo?.Id))
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    var result = new ResponseReslut() { code = 401, msg = "授权过期" };
                    filterContext.HttpContext.Response.StatusCode = 401;
                    filterContext.HttpContext.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result));
                }
                else {
                    //
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = "Login", Action = "Index" }));
                }

            }

            base.OnActionExecuting(filterContext);
        }

        //调用之后执行
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }
    }
}